package com.nondo.dean.product.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductType;
import com.nondo.dean.product.model.ProductTypeBean;
import com.nondo.dean.product.service.ProductTypeService;


/**
 * 产品类型管理
 * @author zlj
 * @date 2018年3月28日
 */
@RestController
@RequestMapping("/{v}/admin/product/type")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 查看产品类型详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<ProductType> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<ProductType> view = new View<ProductType>(20000, "获取数据成功");
        
        ProductType productType = null;
        if (id != null) {
            productType = productTypeService.selectProductType(id);
        } else {
            productType = new ProductType();
        }

        view.setData(productType);
        return view;
    }
    
    /**
     * 添加产品类型
     * @param request
     * @param response
     * @param productType 产品类型信息 {@link ProductType}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ProductType> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody ProductType productType, BindingResult result) {
        View<ProductType> view = new View<ProductType>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductType productTypeDb = this.productTypeService.selectProductTypeByName(productType.getName());
            if (productTypeDb != null) {
                return View.of500Error("产品类型名称已存在");
            }
            this.productTypeService.addProductType(productType);
        }
        return view;
    }

    /**
     * 更新产品类型
     * @param request
     * @param response
     * @param productType 产品类型信息 {@link ProductType}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<ProductType> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody ProductType productType, BindingResult result) {
        View<ProductType> view = new View<ProductType>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductType productTypeDb = productTypeService.selectProductType(productType.getId());
            if (productTypeDb.getName().equals(productType.getName()) && productTypeDb.getId() != productType.getId()) {
                return View.of500Error("产品类型名称已存在");
            }
            BeanUtils.copy(productType, productTypeDb);
            this.productTypeService.updateProductType(productTypeDb);
        }

        return view;
    }

    /**
     * 获取产品类型列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<ProductType>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductType> query) {
        View<Page<ProductType>> view = new View<Page<ProductType>>(20000, "获取列表成功");

        ProductType productType = query.getData();

        Page<ProductType> page = new Page<ProductType>(query.getPage(), query.getLimit(), productTypeService.count(productType));
        
        List<ProductType> productTypeList = productTypeService.getProductTypeList(query.getOffset(), query.getLimit(), productType);
        
        page.setContent(productTypeList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除产品类型
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        ProductType productType = productTypeService.selectProductType(id);
        productType.setDelFlag(String.valueOf(1));
        this.productTypeService.updateProductType(productType);

        return view;
    }
    
    /**
     * 获取所有产品类型列表
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/all")
    public View<List<ProductTypeBean>> list(HttpServletRequest request, HttpServletResponse response) {
        View<List<ProductTypeBean>> view = new View<List<ProductTypeBean>>(20000, "获取列表成功");

        
        List<ProductTypeBean> productTypeList = productTypeService.getProductTypeList();
        
        view.setData(productTypeList);
        
        return view;
    }
    
}
